package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.AddOrNotifyAddressContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.model.Region;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.AddOrNotifyAddressActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by amy on 2017/11/14.
 */

public class AddOrNotifyAddressPresenter implements AddOrNotifyAddressContract.Presenter {

    private AddOrNotifyAddressContract.View mView;
    private AddOrNotifyAddressActivity mContext;

    public AddOrNotifyAddressPresenter(AddOrNotifyAddressActivity activity,
                                       AddOrNotifyAddressActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void selectReceivingAddress(List<Region> regionList,
                                       List<Region.Children.ChildClass> childclassLists,
                                       List<Region.Children> childList) {
        if (regionList.size() != 0) {
            List<Region.Children> childrenList = regionList.get(0).getChildren();
            childList.clear();
            childList.addAll(childrenList);
            if (childList.size() != 0) {
                List<Region.Children.ChildClass> childclassList = childList.get(0).getChildren();
                if (childclassList != null) {
                    childclassLists.clear();
                    childclassLists.addAll(childclassList);
                }
            }
        }
        mView.showAddressPopupWindow(regionList, childList, childclassLists);
    }

    @Override
    public void saveReceivingAddress(ConsumerAddress.ConsumerData consumerAddressBean,
                                     String receivingAddressName, String receivingAddressNumber,
                                     String tvAddressCode, String receivingAddressDetailAddress,
                                     String isMajorAddress) {
        if (consumerAddressBean == null) {
            // 新增地址
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());//消费者id
                jsonObject.put("area_id", tvAddressCode);//区域id
                jsonObject.put("address", receivingAddressDetailAddress);//详细地址
                jsonObject.put("consignee", receivingAddressName);//收货人
                jsonObject.put("mobile", receivingAddressNumber);//收货人手机号
                jsonObject.put("is_major_addr", isMajorAddress);//是否是默认收货地址
            } catch (JSONException e) {
                e.printStackTrace();
            }

            MineApiDataSource.getInstance()
                    .notifyOrAddReceivingAddress(InfoProvider.getAppToken(mContext),
                            "member.consumerAddressCreate", jsonObject.toString())
                    .subscribe(new ResponseObserver<Object>(App.getInstance()) {
                        @Override
                        public void onSuccess(Object o) {
                            // TipUtils.showToast(mContext,"新增地址成功");
                            mView.showSucc("新增地址成功");
                            mView.addOrNotifySuccess(true);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.showError("新增地址失败");
                        }
                    });
        } else {
            // 修改地址
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());//消费者id
                jsonObject.put("address_id", consumerAddressBean.getAddress_id());
                jsonObject.put("area_id", tvAddressCode);//区域id
                jsonObject.put("address", receivingAddressDetailAddress);//详细地址
                jsonObject.put("consignee", receivingAddressName);//收货人
                jsonObject.put("mobile", receivingAddressNumber);//收货人手机号
                jsonObject.put("is_major_addr", isMajorAddress);//是否是默认收货地址
            } catch (JSONException e) {
                e.printStackTrace();
            }

            MineApiDataSource.getInstance()
                    .notifyOrAddReceivingAddress(InfoProvider.getAppToken(mContext),
                            "member.consumerAddressEdit", jsonObject.toString())
                    .subscribe(new ResponseObserver<Object>(App.getInstance()) {
                        @Override
                        public void onSuccess(Object o) {
                            mView.showSucc("修改地址成功");
                            mView.addOrNotifySuccess(false);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.showError("修改地址失败");
                        }
                    });
        }
    }

    private String s_sheng, s_shi, s_qu;

    @Override
    public void changeCode2Address(String tvAddressCode, List<Region> regionList) {
        String s_id = tvAddressCode.substring(0, 2);
        for (int i = 0; i < regionList.size(); i++) {
            Region rg = regionList.get(i);
            if (rg.getId().startsWith(s_id)) {
                s_sheng = rg.getValue();
                List<Region.Children> list_c = rg.getChildren();
                String ss_id = tvAddressCode.substring(0, 4);
                for (int j = 0; j < list_c.size(); j++) {
                    Region.Children r_c = list_c.get(j);
                    if (r_c.getId().startsWith(ss_id)) {
                        s_shi = r_c.getValue();
                        List<Region.Children.ChildClass> list_s = r_c.getChildren();
                        for (int k = 0; k < list_s.size(); k++) {
                            Region.Children.ChildClass r_c_c = list_s.get(k);
                            if (r_c_c.getId().equals(tvAddressCode)) {
                                s_qu = r_c_c.getValue();
                                break;
                            }

                        }
                    }
                }
            }
        }
        if (s_sheng.equals(s_shi)) {
            mView.showAddress(s_sheng + s_qu);
        } else {
            mView.showAddress(s_sheng + "," + s_shi + "," + s_qu);
        }
    }

}
